<template>
  <div>
    <el-dialog title="新增" width="450px"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :visible.sync="dialogVisible"
               :before-close="handleClose" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" class="form-item"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="genderCode">
          <DictSelect clearable v-model="form.genderCode" :type-code="$dictConsts.GENDER" class="form-item"></DictSelect>
        </el-form-item>
        <el-form-item label="医院" prop="hospitalId">
          <el-input v-model="form.hospitalName" readonly class="form-item">
            <el-button slot="append" icon="el-icon-search" @click="hospitalRefVisible=true"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="科室" prop="deptId">
          <el-input v-model="form.deptName" readonly class="form-item">
            <el-button slot="append" icon="el-icon-search" :disabled="!form.hospitalId" @click="deptRefVisible=true"></el-button>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="submitBtnLoading" @click="submit">确定</el-button>
      </div>
    </el-dialog>
    <HospitalRef :visible="hospitalRefVisible"
                 @on-close="hospitalRefVisible=false"
                 @on-select="selectHospital"></HospitalRef>
    <DeptRef :visible="deptRefVisible"
             :hospitalId="form.hospitalId"
             @on-close="deptRefVisible=false"
             @on-select="selectDept"></DeptRef>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import DictSelect from '@/components/form/DictSelect'
import HospitalRef from '@/components/ref/HospitalRef'
import DeptRef from '@/components/ref/DeptRef'

export default {
  name: 'SaveEmployee',
  components: { DeptRef, HospitalRef, DictSelect },
  props: {
    visible: {
      type: Boolean
    }
  },
  data() {
    return {
      dialogVisible: false,
      form: {},
      rules: {
        name: [
          {
            required: true,
            message: '姓名不能为空',
            trigger: 'blur'
          }
        ]
      },
      submitBtnLoading: false,
      hospitalRefVisible: false,
      deptRefVisible: false
    }
  },
  methods: {
    ...mapActions(['SaveEmployee']),
    handleClose(done) {
      this.$refs.form.resetFields()
      this.form = {}
      this.$emit('on-close')
      done()
    },
    close() {
      this.$refs.form.resetFields()
      this.form = {}
      this.$emit('on-close')
      this.dialogVisible = false
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitBtnLoading = true
          this.SaveEmployee(this.form).then(() => {
            this.submitBtnLoading = false
            this.$refs.form.resetFields()
            this.form = {}
            this.$emit('on-success')
            this.dialogVisible = false
          }).catch(() => {
            this.submitBtnLoading = false
          })
        }
      })
    },
    selectHospital(val) {
      this.form.hospitalId = val.id
      this.form.hospitalName = val.name
      this.hospitalRefVisible = false
    },
    selectDept(val) {
      this.form.deptId = val.id
      this.form.deptName = val.name
      this.deptRefVisible = false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.dialogVisible = val
      }
    }
  }
}
</script>

<style scoped>
  .form-item {
    width: 250px;
  }
</style>
